/**
 * Project: TacticaPescaditos
 * File: Defending.java
 */
package org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.fsm.state;

import org.javahispano.javacup.modelo.Constantes;
import org.javahispano.javacup.modelo.Posicion;

import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.Player;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.TacticaPescaditos;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.messages.Telegram;

/**
 * @author Carlos Alberto
 * class: Defending
 */
public class Defending implements State<TacticaPescaditos> {

	public static final State<TacticaPescaditos> instance = new Defending();

	private static final int NUM_REGIONS = 3;

	private static final double THRESHOLD = Constantes.LARGO_CAMPO_JUEGO/(NUM_REGIONS+1);
	
	private Posicion[][] homeRegions;
	
	private void initializeHomeRegions() {
		homeRegions = new Posicion[NUM_REGIONS][];
		
		homeRegions[0]=new Posicion[]{
			            new Posicion(0.0,-52.0),
			            new Posicion(5.8,-42.235),
			            new Posicion(-20.4,-45.235),
			            new Posicion(-5.8,-42.235),
			            new Posicion(20.4,-45.235),
			            new Posicion(17.0,-26.0),
			            new Posicion(0.0,-26.0),
			            new Posicion(-17.0,-26.0),
			            new Posicion(-17.594405594405593,8.076923076923077),
			            new Posicion(0.0,8.0),
			            new Posicion(17.0,8.0)
			        };

		homeRegions[1]=new Posicion[]{
			            new Posicion(0.0,-52.0),
			            new Posicion(7.132867132867133,-15.678733031674208),
			            new Posicion(-24.727272727272727,-13.303167420814479),
			            new Posicion(-9.986013986013985,-15.203619909502263),
			            new Posicion(24.251748251748253,-14.490950226244346),
			            new Posicion(20.685314685314687,15),
			            new Posicion(-0.23776223776223776,15),
			            new Posicion(-21.16083916083916,5.15),
			            new Posicion(-21.636363636363637,20.805429864253394),
			            new Posicion(-0.4755244755244755,20.042986425339365),
			            new Posicion(20.447552447552447,20.468325791855204)
			        };

		homeRegions[2]=new Posicion[]{
			            new Posicion(0.0,-52.0),
			            new Posicion(9.510489510489512,-11.64027149321267),
			            new Posicion(-28.76923076923077,-10.214932126696834),
			            new Posicion(-12.125874125874127,-11.402714932126697),
			            new Posicion(25.202797202797203,-13.065610859728507),
			            new Posicion(22.11188811188811,14.25339366515837),
			            new Posicion(-1.902097902097902,10.690045248868778),
			            new Posicion(-27.58041958041958,15.203619909502263),
			            new Posicion(-16.405594405594407,35.997737556561084),
			            new Posicion(-1.188811188811189,30.6447963800905),
			            new Posicion(14.74125874125874,35.76018099547511)
			        };

	}

	private int currentIndex;
	private Defending(){
		initializeHomeRegions();
	}

	public void enter(TacticaPescaditos o) {
		currentIndex = calculateCurrentIndex(o);

		o.setHomePositions( homeRegions[currentIndex] );
		
	}

	private int calculateCurrentIndex(TacticaPescaditos o) {
		double height = o.getBall().getPosition().y + o.getHeightPitch();
		
		int index = (int) Math.floor( height/THRESHOLD);
		if (index >= homeRegions.length){
			index = homeRegions.length-1;
		} else if (index < 0 ) {
			index=0;
		}
		return index;
	}

	public void execute(TacticaPescaditos o) {
		if (o.isControlling()&& !Player.PLAYER_ROLE.GOAL_KEEPER.equals(o.getControllingPlayer().getPlayerRole()) ){
			o.getStateMachine().changeState(Attacking.instance);
		} else {			
			int index = calculateCurrentIndex(o);
			if (currentIndex != index){
				currentIndex = index;
				o.setHomePositions(homeRegions[currentIndex] );
			}
		}
	}

	public void exit(TacticaPescaditos o) {		
	}

	public boolean onMessage(TacticaPescaditos o, Telegram telegram) {
		return false;
	}

}
